/* Includes ------------------------------------------------------------------*/
#include "msg_box.h"
#include "icons.h"

/* Private macro -------------------------------------------------------------*/

/* Private types -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
msgbox_t msgbox;

/* Private function prototypes -----------------------------------------------*/
void msgbox_btn_cb(lv_event_t *event);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : msgbox_font_init
 * Description    : init message box
 * Input          : [lv_font_t *font_title, lv_font_t *font_text] font
 *                  - [lv_font_t *font_title] title font
 *                  - [lv_font_t *font_text] text font
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void msgbox_font_init(lv_font_t *font_title, lv_font_t *font_text)
{
    msgbox.font_title = font_title;
    msgbox.font_text = font_text;
}

/******************************************************************************
 * Function Name  : show_msgbox
 * Description    : show message box
 * Input          : [char *title, char *text, bool btn_close_en, bool btn_confirm_en, bool btn_cancel_en, bool btn_back_en, msg_cb_t cb, void *args] title, text, button enable, callback and args
 *                  - [char *title] title
 *                  - [char *text] text
 *                  - [bool btn_close_en] close button enable
 *                  - [bool btn_confirm_en] confirm button enable
 *                  - [bool btn_cancel_en] cancel button enable
 *                  - [bool btn_back_en] back button enable
 *                  - [msg_cb_t cb] callback
 *                  - [void *args] args
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_msgbox(char *title, char *text, bool btn_close_en, bool btn_confirm_en, bool btn_cancel_en, bool btn_back_en, msg_cb_t cb, void *args)
{
    int btns = 0;
    int btn_width = 100, btn_height = 40;
    lv_obj_t *btn_confirm = NULL;
    lv_obj_t *btn_cancel = NULL;
    lv_obj_t *btn_back = NULL;
    lv_obj_t *top = lv_layer_top();

    // save msgbox callback handler and args
    msgbox.cb = cb;
    msgbox.args = args;

    lv_obj_add_flag(top, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_color(top, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(top, LV_OPA_50, 0);

    msgbox.box = lv_obj_create(top);
    lv_obj_set_size(msgbox.box, 500, 300);
    lv_obj_set_style_bg_opa(msgbox.box, LV_OPA_COVER, 0);
    lv_obj_set_style_pad_all(msgbox.box, 10, 0);
    lv_obj_align_to(msgbox.box, top, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *lable_title = lv_label_create(msgbox.box);
    if (msgbox.font_title != NULL)
    {
        lv_obj_set_style_text_font(lable_title, msgbox.font_title, 0);
    }    
    lv_label_set_text(lable_title, title);
    lv_obj_align(lable_title, LV_ALIGN_TOP_MID, 0, 0);

    lv_obj_t *lable_text = lv_label_create(msgbox.box);
    if (msgbox.font_text != NULL)
    {
        lv_obj_set_style_text_font(lable_text, msgbox.font_text, 0);
    }
    lv_label_set_text(lable_text, text);
    lv_obj_align(lable_text, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *line = lv_line_create(msgbox.box);
    static lv_point_t line_points[] = {{0, 0}, {475, 0}};
    lv_line_set_points(line, line_points, 2);
    lv_obj_align_to(line, lable_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    if (btn_close_en)
    {
        lv_obj_t *btn_close = lv_btn_create(msgbox.box);
        lv_obj_set_size(btn_close, 30, 30);
        lv_obj_set_style_radius(btn_close, 15, 0);
        lv_obj_set_style_border_width(btn_close, 0, 0);
        lv_obj_set_style_pad_all(btn_close, 0, 0);
        lv_obj_set_style_bg_opa(btn_close, LV_OPA_TRANSP, 0);

        lv_obj_t *label_close = lv_label_create(btn_close);
        if (msgbox.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_close, msgbox.font_title, 0);
        }
        lv_label_set_text(label_close, ICON_XMARK);

        lv_obj_align_to(label_close, btn_close, LV_ALIGN_CENTER, 0, 0);
        lv_obj_align_to(btn_close, msgbox.box, LV_ALIGN_TOP_RIGHT, 0, 0);

        lv_obj_add_event_cb(btn_close, msgbox_btn_cb, LV_EVENT_CLICKED, (void *)1);
    }

    if (btn_confirm_en)
    {
        btn_confirm = lv_btn_create(msgbox.box);
        lv_obj_set_size(btn_confirm, btn_width, btn_height);
        lv_obj_set_style_radius(btn_confirm, 5, 0);
        lv_obj_set_style_bg_color(btn_confirm, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_confirm, 0, 0);

        lv_obj_t *label_confirm = lv_label_create(btn_confirm);
        if (msgbox.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_confirm, msgbox.font_title, 0);
        }
        lv_label_set_text(label_confirm, "确定");
        lv_obj_align(label_confirm, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_confirm, msgbox_btn_cb, LV_EVENT_CLICKED, (void *)2);
        btns++;
    }

    if (btn_cancel_en)
    {
        btn_cancel = lv_btn_create(msgbox.box);
        lv_obj_set_size(btn_cancel, btn_width, btn_height);
        lv_obj_set_style_radius(btn_cancel, 5, 0);
        lv_obj_set_style_bg_color(btn_cancel, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_cancel, 0, 0);

        lv_obj_t *label_cancel = lv_label_create(btn_cancel);
        if (msgbox.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_cancel, msgbox.font_title, 0);
        }
        lv_label_set_text(label_cancel, "取消");
        lv_obj_align(label_cancel, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_cancel, msgbox_btn_cb, LV_EVENT_CLICKED, (void *)3);
        btns++;
    }

    if (btn_back_en)
    {
        btn_back = lv_btn_create(msgbox.box);
        lv_obj_set_size(btn_back, btn_width, btn_height);
        lv_obj_set_style_radius(btn_back, 5, 0);
        lv_obj_set_style_bg_color(btn_back, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_back, 0, 0);

        lv_obj_t *label_back = lv_label_create(btn_back);
        if (msgbox.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_back, msgbox.font_title, 0);
        }
        lv_label_set_text(label_back, "返回");
        lv_obj_align(label_back, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_back, msgbox_btn_cb, LV_EVENT_CLICKED, (void *)4);
        btns++;
    }
    if (btns == 1)
    {
        if (btn_confirm_en)
        {
            lv_obj_align_to(btn_confirm, msgbox.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
        else if (btn_cancel_en)
        {
            lv_obj_align_to(btn_cancel, msgbox.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
        else if (btn_back_en)
        {
            lv_obj_align_to(btn_back, msgbox.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
    }
    else if (btns == 2)
    {
        bool second = false;
        if (btn_confirm_en)
        {
            lv_obj_align_to(btn_confirm, msgbox.box, LV_ALIGN_BOTTOM_MID, -btn_width / 2 - 10, 0);
            second = true;
        }
        if (btn_cancel_en)
        {
            if (second)
            {
                lv_obj_align_to(btn_cancel, msgbox.box, LV_ALIGN_BOTTOM_MID, btn_width / 2 + 10, 0);
            }
            else
            {
                lv_obj_align_to(btn_cancel, msgbox.box, LV_ALIGN_BOTTOM_MID, -btn_width / 2 - 10, 0);
                second = true;
            }
        }
        if (btn_back_en)
        {
            lv_obj_align_to(btn_back, msgbox.box, LV_ALIGN_BOTTOM_MID, btn_width / 2 + 10, 0);
        }
    }
    else
    {
        lv_obj_align_to(btn_cancel, msgbox.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        lv_obj_align_to(btn_confirm, btn_cancel, LV_ALIGN_OUT_LEFT_MID, -20, 0);
        lv_obj_align_to(btn_back, btn_cancel, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
    }
}

/******************************************************************************
 * Function Name  : msgbox_btn_cb
 * Description    : message box button callback
 * Input          : [lv_event_t *event] event
 *                  - [lv_event_t *event] event
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void msgbox_btn_cb(lv_event_t *event)
{
    lv_obj_del(msgbox.box);
    lv_obj_t *top = lv_layer_top();
    lv_obj_clear_flag(top, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_opa(top, LV_OPA_TRANSP, 0);
    int param = (int)lv_event_get_user_data(event);
    if (param == 1)
    {
        // close code callback
    }
    else if (param == 2)
    {
        // confirm code callback
        if (msgbox.cb != NULL)
        {
            msgbox.cb(msgbox.args);
        }
    }
    else if (param == 3)
    {
        // cancel code callback
    }
    else if (param == 4)
    {
        // back code callback
    }
}
/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/

